package exemplo;

import exemplo.db.HibernateUtil;
import exemplo.entidade.Livro;
import java.util.List;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Principal {

    public static void main(String[] args) {
        System.out.println("Exemplo de geração de relatório");
        Session secao = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = secao.beginTransaction();
        List listaLivros = secao.createQuery("from Livro").list();
        tx.commit();
        secao.close();
        
        String caminhoJRXML = "relatorios/ListagemDeLivros.jrxml";
        
        try {
            JasperReport jr = JasperCompileManager.compileReport(caminhoJRXML);
            JRBeanCollectionDataSource jdcd = new JRBeanCollectionDataSource(listaLivros, false);
            JasperPrint jp = JasperFillManager.fillReport(jr, null, jdcd);
            JasperExportManager.exportReportToPdfFile(jp, "relatorios/ListaLivros.pdf");
        } catch(JRException e) {
            //System.out.println(e);
            e.printStackTrace();
        }
        
        System.out.println("Relatorio gerado");
    }
}
